﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using nRoute.Components;
using nRoute.Mapping;
using nRoute.SiteMaps;
using nRoute.ViewModels;
using SimpleShell.Views;
using nRoute.Components.Composition;
using nRoute.ViewServices.Contracts;
using System.Diagnostics;
using SimpleShell.Components;

namespace SimpleShell.ViewModels
{
    [DefineNavigationContent("Content/Pages/About/", typeof(About))]
    [MapViewModel(typeof(About))]
    public class AboutViewModel 
        : NavigationViewModelBase
    {
        private IDisposable _statusToken;
        private Future<IShowStatusViewService> _statusViewService;
        private Future<IShowIndicatorViewService> _indicatorViewService;

        [ResolveConstructor]
        public AboutViewModel(Future<IShowStatusViewService> statusViewService, Future<IShowIndicatorViewService> indicatorViewService)
        {
            _statusViewService = statusViewService;
            _indicatorViewService = indicatorViewService;

            SetupCommands();
        }

        public ActionCommand ShowCommand { get; private set; }

        public ActionCommand HideCommand { get; private set; }

        public string Message { get; set; }

        private void SetupCommands()
        {
            ShowCommand = new ActionCommand(() =>
            {
                if (_statusToken != null) _statusToken.Dispose();
                _statusToken = _statusViewService.Value.ShowStatus("Hello World at " + DateTime.Now.ToLocalTime(), () =>
                    {
                        this.Message = "Status dismissed at " + DateTime.Now.ToLocalTime();
                        NotifyPropertyChanged(() => Message);
                    });
            });

            HideCommand = new ActionCommand(() =>
            {
                if (_statusToken != null) _statusToken.Dispose();
                _statusToken = null;

                var _random = (new Random(DateTime.Now.Millisecond)).Next(2, 15);
                _indicatorViewService.Value.ShowIndicator("Timeout: " + _random.ToString(), TimeSpan.FromSeconds(_random));
            });
        }

        ~AboutViewModel()
        {
            Debug.WriteLine("Finalizing AboutViewModel: " + this.GetHashCode());
        }
    }
}
